package com.ihr360.excel.service.impl;

import com.google.common.collect.Lists;
import com.ihr360.excel.annotation.InterruptIgnore;
import com.ihr360.excel.dao.SpreadSheetRepository;
import com.ihr360.excel.dao.SpreadsheetJournalRepository;
import com.ihr360.excel.model.dto.SpreadSheet;
import com.ihr360.excel.model.dto.SpreadsheetJournal;
import com.ihr360.excel.model.type.JournalType;
import com.ihr360.excel.model.type.SpreadSheetStatus;
import com.ihr360.excel.service.SpreadsheetJournalService;
import com.ihr360.excel.service.SpreadsheetService;
import com.ihr360.excel.vo.ProgressContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SpreadsheetJournalServiceImpl implements SpreadsheetJournalService {

    @Autowired
    private SpreadsheetJournalRepository spreadsheetJournalRepository;

    @Autowired
    private SpreadSheetRepository spreadSheetRepository;

    @Autowired
    private SpreadsheetService spreadsheetService;

    @Override
    @InterruptIgnore
    public List<SpreadsheetJournal> getCurrentInterruptLog(ProgressContext progressContext) {
        SpreadSheet spreadSheet = spreadSheetRepository.findByProgressKeyAndStatus(progressContext.getProgressKey(), SpreadSheetStatus.INPROGRESS);
        if (spreadSheet == null) {
            spreadSheet = spreadsheetService.getOrAndCurrentSpreadsheet(progressContext);
        }
        return spreadsheetJournalRepository.findByTypeInAndProgressKeyAndSheetOrderByRowNum(Lists.newArrayList(JournalType.INTERRUPTLOG), progressContext.getProgressKey(), spreadSheet);
    }
}
